package juc.day01;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 两个线程，一个线程打印1-52，另一个打印字母A-Z，打印顺序为12A34B…5152Z。要求用线程间通信
 */
public class T02 {
    public static void main(String[] args) {
        ShareDataOne2 shareDataOne2 = new ShareDataOne2();
        new Thread(()->{shareDataOne2.printNumber();}).start();
        new Thread(()->{shareDataOne2.printWord();}).start();
    }
}

class ShareDataOne2{
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void printNumber(){
        try {
            lock.lock();
            for(int i=1;i<=52;i++){
                System.out.print(i);
                if(i % 2 == 0){
                    condition.signalAll();
                    condition.await();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void printWord(){
        try {
            lock.lock();
            for (char i = 'A';i<='Z';i++){
                System.out.print(i);
                condition.signalAll();
                condition.await();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
